﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dcms.BLL;
using Dcms.Model;
using Dcms.Common;
using AjaxPro;
using Dcms.UpdateModule;

/// <summary>
/// ManageWebBase 的摘要说明
/// </summary>
public class ManageWebBase : System.Web.UI.Page
{
    public ManageWebBase()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    protected Button btnSave;
    protected Button btnCopy;
    protected Button btnDel;
    protected HiddenField hid_CateID;
    protected HiddenField hid_ParentID;
    protected TextBox txb_CateName;

    public String ROLESTR = "";
    public String Language = "";
    CompetenceLib CL = new CompetenceLib();

    /// <summary>
    /// 页面加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        CL.IsLogin();
        Utility.RegisterTypeForAjax(typeof(Dcms.BLL.Dcms_CateBLL));
        try
        {
            if (Session["DCMSLANGUAGE"] != null)
            {
                Language = Session["DCMSLANGUAGE"].ToString();
            }
            GetModelFlag();

            if (!IsPostBack)
            {
                if (strAction == "edit")
                {
                    if (CL.IsEdit(ROLESTR) == false)
                    {
                        btnCopy.Enabled = false;
                        btnSave.Enabled = false;
                    }
                }
                else
                {
                    btnCopy.Enabled = false;
                    btnSave.Enabled = false;
                    if (CL.IsAdd(ROLESTR) == false)
                    {
                        btnCopy.Enabled = false;
                        btnSave.Enabled = false;
                    }
                }
                if (CL.IsDelete(ROLESTR) == false)
                {
                    btnDel.Enabled = false;
                }
                BindData();
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Manage_Modle_News_EditNews.Page_Load", ex);
            throw ex;
        }
    }

    private void GetModelFlag()
    {
        Dcms_PermissionBLL pBll = new Dcms_PermissionBLL();
        Dcms_PermissionModel pModel = pBll.GetModelByID(Convert.ToInt32(strPermissionID));
        ROLESTR = pModel.Permission_Flag;
    }

    public virtual void BindData()
    {
        if (strCatID != "")
        {
            Dcms_CateBLL cBll = new Dcms_CateBLL();
            Dcms_CateModel cModel = cBll.GetModelByID(Convert.ToInt32(strCatID));
            int ParentId = cBll.GetTopCateIdByFlag(ROLESTR, cModel.Cate_ModelKeyId, Language);
            hid_CateID.Value = cModel.Cate_ID.ToString();
            txb_CateName.Text = cModel.Cate_Name;
            txb_CateName.Enabled = false;
            hid_ParentID.Value = ParentId.ToString();
            if (strAction == "edit")
            {
                btnDel.Visible = true;
                btnCopy.Visible = true;
                Dcms_NewsModel nModel = new Dcms_NewsModel();
                Dcms_NewsBLL nBll = new Dcms_NewsBLL();
                nModel = nBll.GetModelByID(Convert.ToInt32(strID));
                DcmsOperateModel objDcmsOperateModel = new DcmsOperateModel();
                objDcmsOperateModel.InitControl(nModel, this.Form.Controls);
            }
        }
    }

    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            Dcms_NewsBLL nBll = new Dcms_NewsBLL();
            if (strAction == "edit")
            {
                Dcms_NewsModel nModel = nBll.GetModelByID(Convert.ToInt32(strID));
                DcmsOperateModel objDcmsOperateModel = new DcmsOperateModel();
                objDcmsOperateModel.UpdateModelByControls(nModel, this.Form.Controls);
                nBll.Update(nModel);
                Session.Add("Message", "信息已经编辑成功，请确认！");
                Session.Add("OtherMessage", "发布信息");
            }
            else
            {
                Dcms_NewsModel nModel = new Dcms_NewsModel();
                DcmsOperateModel.InitModel(nModel);
                nModel.News_CateId = Convert.ToInt32(hid_CateID.Value);
                DcmsOperateModel objDcmsOperateModel = new DcmsOperateModel();
                objDcmsOperateModel.UpdateModelByControls(nModel, this.Form.Controls);
                nBll.AddModel(nModel);
                Session.Add("Message", "信息已经添加成功，请确认！");
                Session.Add("OtherMessage", "继续添加");
            }
            Session.Add("Url", "Modle/News/NewsList.aspx?id=" + strPermissionID + "&TopCateID=" + strTopCateID);
            Dcms_CateBLL cBll = new Dcms_CateBLL();
            Dcms_CateModel cModel = cBll.GetModelByID(Convert.ToInt32(strCatID));
            Session.Add("OtherUrl", "Modle/News/EditNews" + cModel.Cate_ModelKeyId + ".aspx?PermissionID=" + strTopCateID + "&TopCateID=" + strTopCateID + "&CatID=" + Convert.ToInt32(hid_CateID.Value) + "&action=add");
            Response.Redirect("../../Message.aspx", false);
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Manage_Modle_News_EditNews.btnSave_Click", ex);
            throw ex;
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        try
        {
            if (CL.IsDelete(ROLESTR))
            {
                Dcms_NewsBLL nBll = new Dcms_NewsBLL();
                nBll.DeleteModelByID(Convert.ToInt32(strID));
                Session.Add("Message", "信息已经删除成功，请确认！");
                Session.Remove("OtherMessage");
                Session.Add("Url", "Modle/News/NewsList.aspx?id=" + strPermissionID + "&TopCateID=" + strTopCateID);
                Response.Redirect("../../Message.aspx", false);
            }
            else
            {
                Session.Add("Message", "你没有删除权限，删除失败，请确认！");
                Session.Add("Url", "Modle/News/NewsList.aspx?id=" + strPermissionID + "&TopCateID=" + strTopCateID);
                Session.Remove("OtherMessage");
                Response.Redirect("../../Message.aspx", false);
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Manage_Modle_News_EditNews.btnDel_Click", ex);
            throw ex;
        }
    }

    /// <summary>
    /// 复制
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCopy_Click(object sender, EventArgs e)
    {
        try
        {
            Dcms_NewsBLL nBll = new Dcms_NewsBLL();
            Dcms_NewsModel nModel = nBll.GetModelByID(Convert.ToInt32(strID));
            DcmsOperateModel objDcmsOperateModel = new DcmsOperateModel();
            objDcmsOperateModel.UpdateModelByControls(nModel, this.Form.Controls);
            nBll.AddModel(nModel);
            Session.Add("Message", "信息已经复制成功，请确认！");
            Session.Remove("OtherMessage");
            Session.Add("Url", "Modle/News/NewsList.aspx?id=" + strPermissionID + "&TopCateID=" + strTopCateID);
            Response.Redirect("../../Message.aspx", false);
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Manage_Modle_News_EditNews.btnCopy_Click", ex);
            throw ex;
        }
    }


    #region 属性
    public string strID
    {
        get
        {
            return Request.QueryString["ID"] + "";
        }
    }

    public string strAction
    {
        get
        {
            return Request.QueryString["action"] + "";
        }
    }

    public string strCatID
    {
        get
        {
            return Request.QueryString["CatID"] + "";
        }
    }

    public string strPermissionID
    {
        get
        {
            return Request.QueryString["PermissionID"] + "";
        }
    }

    public string strTopCateID
    {
        get
        {
            return Request.QueryString["TopCateID"] + "";
        }
    }
    #endregion
}